'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	let apiUrl = 'http://apis.juhe.cn/fapig/birthdayFlower/query'
	let date = new Date();
	let month = date.getMonth() + 1;
	let strDate = date.getDate();
	const {
		data
	} = await uniCloud.httpclient.request(apiUrl, {
		method: 'GET',
		data: {
			keyword: `${month}-${strDate}`,
			key: '606bc1268fceb2e8bae390b9affc1014',
		},
		contentType: 'json',
		dataType: 'json'
	})
	if (data.reason === 'SUCCES') {
		data.result.name_des=data.result.name_des.replace(/<\/p>/g, '').replace(/<p>/g, '')
		data.result.lang_des=data.result.lang_des.replace(/<\/p>/g, '').replace(/<p>/g, '')
		data.result.stone_des=data.result.stone_des.replace(/<\/p>/g, '').replace(/<p>/g, '')
		data.result.legend=data.result.legend.replace(/<\/p>/g, '').replace(/<p>/g, '')
		let res = await db.collection('shengricode').add({
			"title":data.result.title,
			"birthday":data.result.birthday,
			"name":data.result.name,
			"name_des":data.result.name_des,
			"lang":data.result.lang,
			"lang_des":data.result.lang_des,
			"stone":data.result.stone,
			"stone_des":data.result.stone_des,
			"legend":data.result.legend,
			"date":+new Date()
		})
		return res
	}
};
